package com.shycloud.mido.member.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.ErpActivityInfo;
import com.shycloud.mido.member.service.ActivityService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.jdom.JDOMException;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author Xiaoxing Yu
 * @date 2020/6/2
 **/
@ApiIgnore
@RestController
@AllArgsConstructor
@RequestMapping("/activity")
@ApiSort(2)
public class ActivityController {

	/** 打印活动 service */
	private ActivityService activityService;

	/**
	 * 增加一个活动
	 *
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */
	@PostMapping("/addActivity")
	public R addActivity(@RequestBody ErpActivityInfo erpActivityInfo) throws IOException, JDOMException {
		return activityService.addActivity(erpActivityInfo);
	}

	/**
	 * 删除一个活动
	 *
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */
	@GetMapping("/delActivity")
	public R delAllActivity(Integer id) throws IOException, JDOMException {
		return activityService.delAllActivity(id);
	}

	/**
	 * 查询活动列表
	 *
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */
	@GetMapping("/getAllAct")
	public R getAllActivity() throws IOException, JDOMException {
		return activityService.getAllActivity();
	}

	/**
	 * 禁用/启用活动
	 *
	 * @param map
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/11 10:53
	 */
	@PostMapping("/updActivity")
	public R updActivity(@RequestBody Map<String, Object> map) {
		//根据ID获取打印活动信息
		ErpActivityInfo activityInfo = activityService.getById((Integer) map.get("id"));

		//更新时间
		activityInfo.setUpdateTime(LocalDateTime.now());
		//状态
		activityInfo.setIsEnabled((String) map.get("isEnabled"));

		return new R(activityService.updateById(activityInfo));
	}

}
